package com.edu.cuotibook.aop;

import com.edu.cuotibook.entity.common.CodeMsg;
import com.edu.cuotibook.entity.common.ResultDTO;
import com.edu.cuotibook.service.MemeryRoom;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

/**
 * create by sym  2021/3/24 21:13.
 * 加锁
 */
//@Aspect
public class AspectLock {
    @Pointcut("within(com.edu.cuotibook.controller.*)")
    public void pointCut(){

    }

    @Around("pointCut()")
    public Object lock(ProceedingJoinPoint joinPoint) throws Throwable{
        if (MemeryRoom.lock.get() == 0) {
            return new  ResultDTO(CodeMsg.TOO_FAST);
        }
        MemeryRoom.lock.incrementAndGet();
        Object proceed = joinPoint.proceed();
        MemeryRoom.lock.decrementAndGet();
        return proceed;
    }
}
